﻿/*
 * Summary:
 *   The FileReader class loads data from text file 
 *   and converts it to corresponding object for
 *   the sports store
 *   
 * History:
 *   Date           Author          Changes
 *   18/10/2010     YIWEI YU        Created
 *   31/10/2010     WEICAN WANG     Implemented the sports items reader 
 *                                  for basic file format
 *   01/11/2010     YIWEI YU        modified the sports items reader
 *                                  for reading enhanced file format
 */
//P487 Throwing Exceptions


using System;
using System.Collections.Generic;
using System.Collections;  /* WEICAN WANG(WCW): for using class Arraylist */
using System.Linq;
using System.Text;
using System.IO;    /* YIWEI YU: for file/stream manipulation */

namespace AcmeSportsStore
{
    class FileReader
    {
        private int i;  
        private string[] splits;
        private string line;
        ArrayList sportItemsArray = new ArrayList();
        ArrayList CustomerInfoArray = new ArrayList();
        StreamReader sr;
        
        /*
         *  constructor of FileReader
         */
        public FileReader(string fileName)
        {
            sr = new StreamReader(fileName);
        }

        public FileReader()
        {
            /*
             * for test
             * this file should be located in the same folder
             * as the executable file is
             */
            sr = new StreamReader("sportsItemsV2.txt");
        }

        public SportsStore ReadSportsItems()
        {
            SportsStore itemsIndexs = new SportsStore();
            this.line = sr.ReadLine();
            i = 0;
            while (line != null)
            {
                // Split out parts and create a sportItem object
                Console.WriteLine("The {0} line:", i);
                this.splits = line.Split(';');
                // sportItemsArray.Add(new SportsItem(splits[0], splits[1], splits[2], Convert.ToInt32(splits[3])));

                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }

                char idinit = splits[0][0];

                if (splits[0][0] == 'M')
                {   /* items without user gender specified */
                    itemsIndexs.AddItem(new SportsItem(
                        splits[0],
                        splits[1],
                        splits[2],
                        (int)(Convert.ToDecimal(splits[3]) * 100 + 0.5m)));
                }
                else if (splits[0][0] == 'G')
                {
                    SportsItemG.Gender gender;
                    if (splits[2] == "male")
                    {
                        gender = SportsItemG.Gender.Male;
                    }
                    else if (splits[2] == "female")
                    {
                        gender = SportsItemG.Gender.Female;
                    }
                    else
                    {
                        gender = SportsItemG.Gender.None;
                    }

                    itemsIndexs.AddItem(new SportsItemG(
                        splits[0],
                        splits[1],
                        splits[3],
                        (int)(Convert.ToDecimal(splits[4]) * 100 + 0.5m),
                        gender));
                }
                else
                {   /* invalid ID, just ignore */
                }

               this.line = sr.ReadLine();
               i++;
            }
            sr.Close();
            i = 0;
            return itemsIndexs;
            throw new NotImplementedException();
        }

        /*
         * Load customer information from a stream
         */
        public Customers ReadCustomers(StreamReader sr)
        {
            throw new NotImplementedException();
        }
    }
}
